using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SmartContentUI.Containers
{
    public partial class ContainerTree : UserControl
    {
        #region Variables
        public delegate void OpenItem(object sender);
        public delegate void SelectItem(object sender);
        public delegate void AddContent(char type, int parent);
        public delegate void EditCont(int contentId);
        public event OpenItem Open;
        public event SelectItem Selected;
        public event AddContent NewContent;
        public event EditCont EditContent;

        private SmartContentBusiness.Session ses;

        #endregion

        #region Constructor
        public ContainerTree(ImageList imgLst, SmartContentBusiness.Session ses)
        {
            InitializeComponent();
            this.container_tv.ImageList = imgLst;
            this.ses = ses;
            if(this.ses.getUser().getClientType() == SmartContentDB.Interfaces.User.ClientType.Administrator)
                this.container_tv.ContextMenuStrip = this.contextMenuStrip1;
        }
        #endregion
        
        #region Form events
        private void container_tv_AfterSelect(object sender, TreeViewEventArgs e)
        {
            Selected(container_tv.SelectedNode);
            Console.WriteLine("container_tv_AfterSelect");
        }

        private void container_tv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Open(sender);
            Console.WriteLine("container_tv_MouseDoubleClick");
        }
        void container_tv_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if(e.KeyChar == (char)Keys.Enter)
            {
                this.container_tv.SelectedNode.Expand();
                Open(sender);
            }
            Console.WriteLine("container_tv_KeyPress");
        }

        private void contenedorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.container_tv.SelectedNode != null)
                NewContent('c', (int)this.container_tv.SelectedNode.Tag);
            else
                NewContent('c', -1);
            Console.WriteLine("contenedorToolStripMenuItem_Click");
        }

        private void expedienteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.container_tv.SelectedNode != null)
                NewContent('f', (int)this.container_tv.SelectedNode.Tag);
            else
                NewContent('c', -1);
            Console.WriteLine("expedienteToolStripMenuItem_Click");
        }

        private void editarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditContent((int)this.container_tv.SelectedNode.Tag);
            Console.WriteLine("editarToolStripMenuItem_Click");
        }

        private void borrarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            TreeNode tv = this.container_tv.SelectedNode;
            if (tv != null)
            {
                if (tv.ImageIndex == 0)
                {
                    this.agregarContenedorToolStripMenuItem.Enabled = true;
                }
                else
                {
                    this.agregarContenedorToolStripMenuItem.Enabled = false;
                }
            }
        }
        #endregion

        #region Functions
        public void createTree()
        {
            this.container_tv.Nodes.Clear();
            SmartContentDB.Interfaces.Content[] content = ses.getContent(-1);
            if (content != null)
            {
                for (int i = 0; i < content.Length; i++)
                {
                    TreeNode tvNode = new TreeNode(content[i].getName(), 0, 0);
                    tvNode.Tag = content[i].getId();
                    tvNode.Name = content[i].getTableName();
                    tvNode.ToolTipText = content[i].getDescription();
                    createSubTree(tvNode);
                    this.container_tv.Nodes.Add(tvNode);
                }
            }
        }
        public void navigate2Item(TreeNode current, int id)
        {
            TreeNodeCollection list;
            if (current != null)
                list = current.Nodes;
            else
                list = container_tv.Nodes;
            for (int i = 0; i < list.Count; i++)
            {
                if ((int)list[i].Tag == id)
                {
                    container_tv.SelectedNode = list[i];
                    container_tv.Select();
                }
                else
                {
                    navigate2Item(list[i], id);
                }
            }
        }

        private void createSubTree(TreeNode parent)
        {
            SmartContentDB.Interfaces.Content[] content = ses.getContent((int)parent.Tag);

            if (content != null)
            {
                for (int i = 0; i < content.Length; i++)
                {
                    TreeNode tvNode = new TreeNode(content[i].getName(), content[i].getType() == SmartContentDB.Interfaces.Content.ContentType.Cab ? 0 : 2,
                        content[i].getType() == SmartContentDB.Interfaces.Content.ContentType.Cab ? 0 : 2);
                    tvNode.Tag = content[i].getId();
                    tvNode.Name = content[i].getTableName();
                    tvNode.ToolTipText = content[i].getDescription();
                    createSubTree(tvNode);
                    parent.Nodes.Add(tvNode);
                }
            }
        }
        #endregion
        
    }
}
